<template>
	<!-- 客户列表 -->
	<div class="content" v-if="!is_detail">
		<div class="content-top">
			客户管理_客户查询
		</div>
		<div class="content-operation">
			<el-button type="primary" icon="el-icon-plus" @click='is_add = true' v-if="has_add">添加客户</el-button>
		</div>
		<div class="content-search">
			<el-form :inline="true" class="demo-form-inline" style="margin-top: 20px;">
				<el-form-item label="客户姓名">
					<el-input v-model="client_name" clearable placeholder="客户姓名"></el-input>
				</el-form-item>
				<el-form-item label="证件号码">
					<el-input v-model="client_num" clearable placeholder="证件号码"></el-input>
				</el-form-item>
				<el-form-item label="联系电话">
					<el-input v-model="client_phone" clearable placeholder="联系电话"></el-input>
				</el-form-item>
				<el-form-item>
				<el-button type="primary" @click='search' icon="el-icon-search">搜索</el-button>
				</el-form-item>
				<el-form-item >
					<el-button v-if="!is_more  && user_type == 0" style="border: none;" @click="get_more" icon="el-icon-caret-bottom"></el-button>
					<el-button v-if="is_more" style="border: none;" @click="get_more" icon="el-icon-caret-top"></el-button>
				</el-form-item>
			</el-form>
			<el-form :inline="true" class="demo-form-inline" v-if="is_more">
				<el-form-item label="所属企业">
					<el-select v-model="client_enter" clearable placeholder="请选择">
						<el-option v-for="item in enter_list" :key="item.id" :label="item.enterpriseName" :value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
		</div>
		<div class="content-table">
			<el-table :data="tableData" show-header stripe style="width: 100%;">
				<el-table-column prop="chineseName" label="姓名" width="250">
				</el-table-column>
				<el-table-column prop="identityType" label="证件类型">
					<template slot-scope="scope">
						<span v-if="scope.row.identityType == item.value" v-for='item in dict.identityType'>{{item.type}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="identityNum" label="证件号码" width="200">
				</el-table-column>
				<el-table-column prop="enterpriseName" label="企业名称" width="250">
				</el-table-column>
				<el-table-column prop="roles" label="角色名称" width="280">
					<template slot-scope="scope">
						<template v-for='item in scope.row.roles'>{{item.roleName}} &nbsp;&nbsp;
						</template>
					</template>
				</el-table-column>
				<el-table-column prop="mobileNum" label="联系电话">
				</el-table-column>
				<el-table-column prop="userStatus" label="状态">
					<template slot-scope="scope">
						<span v-if="scope.row.userStatus == item.value" v-for='item in dict.userStatus'>{{item.type}}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button @click="showdetail(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-view"></i></el-button>
						<el-button @click="handleClick(scope.row)" v-if="has_edit" type="text" size="small"><i class="el-icon-edit"></i></el-button>
						<el-button @click="handleClick1(scope.row)" v-if="has_del" type="text" size="small"><i class="el-icon-delete"></i></el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<div class="content-page">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="page"
			 :page-size=limit layout="prev, pager, next, jumper" :total=total>
			</el-pagination>
		</div>


		<template v-if="is_add">
			<div class="all-screen"></div>
			<div class='add-box'>
				<ClientAdd @saveAdd='saveAdd'></ClientAdd>
			</div>
		</template>
		<template v-if="is_edit">
			<div class="all-screen"></div>
			<div class='add-box'>
				<ClientEdit @saveEdit='saveEdit' :id='id'></ClientEdit>
			</div>
		</template>
	</div>
	<div v-else class="content">
		<div class="content-top">
			客户管理_客户详情
		</div>
		<div class="enter-title">
			<div class="enter-title-left">基本信息</div>
			<div class="enter-title-right" @click="is_edit = true"><i class="el-icon-edit"></i>修改</div>
		</div>
		<div class="enter-detail">
			<el-row>
				<el-col :span="8">姓名：{{info.chineseName}}</el-col>
				<el-col :span="8">证件类型：<template v-if="item.value == info.identityType" v-for='item in dict.identityType'>{{item.type}}
					</template></el-col>
				<el-col :span="8">证件号：{{info.identityNum}}</el-col>
			</el-row>
			
			<el-row>
				<el-col :span="8">年龄：{{info.age}}岁</el-col>
				<el-col :span="8">所属企业：{{info.enterpriseName}}</el-col>
				<el-col :span="8">邮箱：{{info.email}}</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">角色：<template v-for='item in info.roles'>{{item.roleName}} &nbsp;&nbsp;
					</template></el-col>
				<el-col :span="8">联系电话：{{info.mobileNum}}</el-col>
			</el-row>
			<el-row>
				<el-col :span="8">系统用户名：{{info.username}}</el-col>
				<el-col :span="8">用户状态：<template v-if="item.value == info.userStatus" v-for='item in dict.userStatus'>{{item.type}}
					</template></el-col>
				<el-col :span="8">数据权限：<template v-if="item.value == info.dataAuth" v-for='item in dict.dataAuth'>{{item.type}}
					</template></el-col>
			</el-row>
		</div>
	
		<div class="enter-title">
			<center>
				<el-button type="danger" @click='handleClick1({"id":id})' icon="el-icon-delete"></el-button>
				<el-button type="primary" @click='show_list'><i class="el-icon-back"></i></el-button>
			</center>
		</div>
		<template v-if="is_edit">
			<div class="all-screen"></div>
			<div class='add-box'>
				<ClientEdit @saveEdit='saveEdit' :id='id'></ClientEdit>
			</div>
		</template>
	</div>
</template>

<script>
	import ClientAdd from '@/components/client/ClientAdd.vue'
	import ClientEdit from '@/components/client/ClientEdit.vue'
	import Global from '@/components/Global.vue'
	let _this;
	export default {
		components: {
			ClientAdd: ClientAdd,
			ClientEdit: ClientEdit,
		},
		props: ['power'],
		data() {
			return {
				client_name:'',
				client_num:'',
				client_phone:'',
				client_enter:'',
				dict: '',
				id: 'id',
				is_add: false,
				is_edit: false,
				is_detail:false,
				is_more:false,
				tableData: [],
				limit: 10,
				page: 1,
				total: 1,
				has_add: false,
				has_edit: false,
				has_del: false,
				info:'',
				enter_list:[],
				user_type:'',

			}
		},
		created() {
			_this = this;
		},
		mounted() {
			this.user_type = localStorage.getItem('type');
			this.check_power();
			this.get_dict();
			this.get_list();
			this.get_enter_list();
		},
		methods: {
			get_enter_list(){
				this.$axios.post(this.$global.serverIp + 'jianeng-admin/sysEnterprise/findPage', {
						"limit": "1000",
						"page": "1",
					})
					.then(function(res) {
						if (res != '') {
							_this.enter_list = res.data.list;
						}
					})
			},
			get_more(){
				this.is_more = !this.is_more;
				this.client_enter = '';
			},
			show_list(){
				this.is_detail = false;
				this.info = {};
			},
			get_dict() {
				this.$axios.get(this.$global.serverIp + 'admin/dict/findDict?keyword=sysClient')
					.then(function(res) {
						if (res != '')
							_this.dict = res.data;
					})

			},
			check_power() {
				this.power.forEach(function(element) {
					if (element.permissionName.indexOf("添加") != -1) _this.has_add = true;
					if (element.permissionName.indexOf("编辑") != -1) _this.has_edit = true;
					if (element.permissionName.indexOf("删除") != -1) _this.has_del = true;
				});
			},
			search() {
				this.page = 1;
				this.get_list();
			},
			get_list() {
				this.$axios.post(this.$global.serverIp + 'admin/sysClient/findPage', {
						"limit": _this.limit + "",
						"page": _this.page + "",
						"chineseName": _this.client_name,
						"identityNum": _this.client_num,
						"mobileNum": _this.client_phone,
						"enterpriseId": _this.client_enter,
					})
					.then(function(res) {
						if (res != '') {
							_this.tableData = res.data.list;
							_this.total = res.data.totalCount;
							_this.page = res.data.currPage;
						}
					})
			},
			saveAdd(type) {
				if (type == 2) {
					this.get_list();
				}
				this.is_add = false;
			},

			saveEdit(type) {
				if (type == 2) {
					this.get_list();
					this.is_detail = false;
				}
				this.is_edit = false;
			},
			handleSizeChange(val) {

				console.log(`每页 ${val} 条`);
			},
			handleCurrentChange(val) {
				this.page = val;
				this.get_list();
				console.log(`当前页: ${val}`);
			},
			showdetail(row) {
				this.id = row.id;
				this.get_info();
				this.is_detail = true;
			},
			get_info(){
				this.$axios.get(this.$global.serverIp + 'admin/sysClient/detail/' + this.id)
					.then(function(res) {
						if (res != '') {
							_this.info = res.data;
						}
				
					})
			},
			handleClick(row) {
				this.id = row.id;
				this.is_edit = true;
			},
			handleClick1(row) {
				this.delopen(row);
			},
			delopen(row) {
				this.$confirm('此操作将删除该客户, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					$.ajax({
						url: Global.serverIp + 'jianeng-admin/sysClient/delete',
						data: '["' + row.id + '"]',
						xhrFields: {
							withCredentials: true
						},
						beforeSend: function(request) {
							request.setRequestHeader("Authorization", 'Bearer ' + localStorage.getItem('token'));
						},
						contentType: 'application/json;charset=utf-8',
						dataType: 'json',
						type: 'delete',
						success: function(res) {
							if (res.code == Global.success_code) {
								console.log(res);
								_this.$popup.open("删除成功");
								_this.get_list();
								_this.is_detail = false;
							} else {
								_this.$popup.open(res.msg);
							}
						},
						error: function(res) {
							if (res.responseJSON.code == '040005') {
								_this.$popup.open("登录过期");
							} else {
								_this.$popup.open("请求失败");
							}
						}
					})
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			},
		},

	}
	import '@/assets/css/common.css'
</script>
<style>
	.enter-title {
		margin: 26px 0px 0px 30px;
		overflow: hidden;
		font-size: 16px;
		width: 1088px;
	}
	
	.enter-title-left {
		float: left;
		color: #606B82;
		margin-bottom: 16px;
	}
	
	.enter-title-right {
		float: right;
		color: #FD943F;
		margin-bottom: 10px;
	}
	
	.enter-title-right:hover {
		cursor: pointer;
		opacity: 0.3;
	}
	
	.enter-detail {
		width: 1088px;
		height: 300px;
		background-color: #F2F6FA;
		border: 1px solid #C9CDD1;
		padding-left: 30px;
		box-sizing: border-box;
		margin-left: 30px;
		padding-top: 40px;
	}
	
	.enter-detail .el-row {
		margin-bottom: 30px;
		font-size: 14px;
		color: #2D384C;
	}
	
</style>

